import {join} from 'path'
import {app} from "electron";
import {IJsonTable, JsonDB, TableConfig} from "../../jsondb/src";

const db: JsonDB = new JsonDB({
    name: 'data',
    path: join(app.getPath('userData'), 'data')
})

export abstract class BaseDatabase {

    protected tableName: string = ''

    protected table: IJsonTable | undefined

    /**
     * 创建数据表
     * @param tableConfig 数据表配置
     */
    async createTable(tableConfig: TableConfig) {
        this.tableName = tableConfig.name;
        const res = await db.createTable(tableConfig)
        if (!res.success) {
            throw new Error(res.error)
        }
        this.table = db.table(tableConfig.name)!
    }
}
